Hi there! If you are looking for exact way to regenerate or redownload the already generated key-pair of your AWS EC2 instance then, I will apologize and would like to let you know in very beginning of this article that, I haven't found any such way.
However, if you want to have a new EC2 key-pair without losing any data of your EC2 instance, then this article will definitely help you.
👉🏻 Time Saver >>> In case you already have good experience with AWS. Then you can create an AMI, and then you can launch an instance from that AMI. Here will not lose anything and get a new key-pair as well.
Here, I'm going to mention the process in a step-wise manner by putting an example. So, in case someone is referring this for learning purpose only, even then he can get most out of this reading.
Old EC2 instance (marvel) - I will treat it like its key-pair has been lost
image 1
You can see in the above image, I have a directory named test
which contains two files named test.py
and test.sh
Create an AMI (Amazon Machine Images) of existing EC2 instance
- Open EC2 services then, go to
instances
section - Select required
instance
- Click on
Actions
dropdown button - Go to
Image and templates
- Select
Create image
and it will take you to the AMI creation flow, likeimage 3
below image 2
AMI creation flow
- Provide
Image name
then, - Provide
Image description
(optional) - You can go with default options for rest of the steps, or you can include/exclude your additional volumes.
image 3
You can add tags
also to your image. Now, move ahead by clicking on Create Image
button and it will take you back to the instances
page. You will get a message for your AMI creation status, like - image 5
image 4
image 5
If I check my AMI's status then, it will show as pending because my instance is running.
image 6
Now, I have to stop my instance. So, that AMI will get created
- Go to
instances
- Select required
instance
- Open
Instance State
dropdown - Select
Stop instance
image 7
Now, on the AMI page you will find your AMI's updated status
Once your AMI's status will become available then it can be used for launching new instances. Until AMI's status becomes available, please do not make any modification to your EC2 instance. (Obviously you have to select the required one in case you have multiple AMIs)
image 8
You can launch an instance by clicking on the "Launch instance from AMI" button in the top right corner, or you can go to EC2 >>> instances >>> Launch an instance
. Here you will find "My AMIs" tab alongside "Quick Start" tab under "Application and OS images" section. simply select the respective image as mentioned in image 9
image 9
Under "Key pair" section, you have to create a new Key Pair
image 10
Simply put a name for your key pair
(Generally I go with "Key pair type" = "RSA" and "Private key file format" = ".pem". However, you can proceed as per your wish) Now, click on "Create key pair" and it will download the "private key file".
image 11
Now launch the instance
image 12
You can see in below image 13
a new instance has been created and initializing.
(If only 2 instances confusing you then let me tell you, in the beginning of this article I had 2 instances one of them was terminated so by the time that one gone. That's why even after creating a new instance you are seeing only two instead of 3)
image 13
Now let me show you that my data from older instance (marvel) has been carried to this instance or not
image 14
All the data has been carried successfully.
Congrats 👏🏻 Now you will be able to do the same...